package com.is2.patchinglife.activity;

import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.telephony.PhoneStateListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.is2.patchinglife.R;
import com.is2.patchinglife.manager.ActivityManager;
import com.is2.patchinglife.manager.ListenerManager;
import com.is2.patchinglife.manager.LocalDataManager;

public class ConfActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// load activity layout
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conf);

		// get sensor list layout
		LinearLayout cslist = (LinearLayout) findViewById(R.id.confSensorList);
		
		// get device sensor list
		SensorManager sensor_manager = ListenerManager.getSensorManager();
		List<Sensor> sList = sensor_manager.getSensorList(Sensor.TYPE_ALL);
		
		// get inflater
		LayoutInflater infalInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listElement;
		
		// add sensor list element 
		for(Sensor s:sList){
			final int type = s.getType();									// get sensor type [int]
			final String sensName = ListenerManager.getSensorType(type);	// convert type into string
	        
			// valid sensor check
			if(sensName!=null){
				listElement = infalInflater.inflate(R.layout.config_child,null);			// get clean layout
				
				TextView sensor_tw = (TextView) listElement.findViewById(R.id.sensorName);	// set name
		        sensor_tw.setText(sensName);
		        
		        TextView power_tw = (TextView) listElement.findViewById(R.id.sensorPower);	// set power
		        power_tw.setText("POWER: "+s.getPower()+" mA");
		       
		        // setting sensor toogle button
		        ToggleButton onoffBtn = (ToggleButton) listElement.findViewById(R.id.onoffBtn);
		        onoffBtn.setChecked(!LocalDataManager.getInhibited(sensName.toLowerCase(Locale.ENGLISH)));	// check saved configuration
		        onoffBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		            public void onCheckedChanged(CompoundButton onoffBtn, boolean isChecked) {
		                if(isChecked){
		                	ListenerManager.removeSensorListener(type);
		                	LocalDataManager.setInhibited(sensName.toLowerCase(Locale.ENGLISH),false);
		                }else {
		                	int[] temp = {type};
		                	ListenerManager.addSensorListener(temp);
		                	LocalDataManager.setInhibited(sensName.toLowerCase(Locale.ENGLISH),true);
		                }
		            }
		        });
		        cslist.addView(listElement);	// add element to list
	        }
		}
		
		// add telephony element
		final int type = PhoneStateListener.LISTEN_CALL_STATE;			// get telephony type [int]
		final String telName = ListenerManager.getTelephonyType(type);	// convert type into string
		
		if(telName!=null){
			listElement = infalInflater.inflate(R.layout.config_child,null);				// get clean layout
			
			TextView tel_tw = (TextView) listElement.findViewById(R.id.sensorName);			// set name
	        tel_tw.setText(telName);
	        
	        TextView power_tw = (TextView) listElement.findViewById(R.id.sensorPower);		// set power
	        power_tw.setText("POWER: 0.0 mA");
			
	        // setting telephony toggle button
			ToggleButton telBtn = (ToggleButton) listElement.findViewById(R.id.onoffBtn);
	        telBtn.setChecked(!LocalDataManager.getInhibited(telName.toLowerCase(Locale.ENGLISH)));		// check saved configuration
	        telBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	            public void onCheckedChanged(CompoundButton onoffBtn, boolean isChecked) {
	                if(isChecked){
	                	ListenerManager.removeTelephonyListener(type);
	                	LocalDataManager.setInhibited(telName.toLowerCase(Locale.ENGLISH),false);
	                }else {
	                	int[] temp = {type};
	                	ListenerManager.addTelephonyListener(temp);
	                	LocalDataManager.setInhibited(telName.toLowerCase(Locale.ENGLISH),true);
	                }
	            }
	        });
	        cslist.addView(listElement);	// add element to list
        }
	        
		// setting sound_effects button
		ToggleButton effBtn = (ToggleButton) findViewById(R.id.effectsBtn);
		effBtn.setChecked(LocalDataManager.getEffects());
		effBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton effBtn, boolean isChecked) {
                if(isChecked){
                	LocalDataManager.setEffects(true);
                }else {
                	LocalDataManager.setEffects(false);
                }
            }
        });
		
		// setting sound_effects button
		ToggleButton vibBtn = (ToggleButton) findViewById(R.id.vibrationBtn);
		vibBtn.setChecked(LocalDataManager.getVibration());
		vibBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton vibBtn, boolean isChecked) {
                if(isChecked){
                	LocalDataManager.setVibration(true);
                }else {
                	LocalDataManager.setVibration(false);
                }
            }
        });
		
		// setting profile button
		FrameLayout profile = (FrameLayout) findViewById(R.id.confProfile);
		profile.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				Intent profile_intent = new Intent(ConfActivity.this, ProfileActivity.class);
				startActivity(profile_intent);
			}
		});
	}
	 @Override
	    public void onDestroy(){
	        super.onDestroy();
	      
	        /*
	         * Aggiornamento ActivityManager
	         */
			String[] aList = {"ConfigActivity"};
		 	ActivityManager.getInstance().closeActivities(aList,false);
		}
	
}
